package cn.gxa.j229.bug.finance.service.borrowing.impl;

import cn.gxa.j229.bug.finance.mapper.InvestmentRecordMapper;
import cn.gxa.j229.bug.finance.mapper.SignMapper;
import cn.gxa.j229.bug.finance.pojo.InvestmentRecord;
import cn.gxa.j229.bug.finance.pojo.Loan;
import cn.gxa.j229.bug.finance.service.borrowing.AllBorrowingSignService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author FanJiang
 * @Date 2020/9/28
 * @Version 1.0
 */

@Service("allBorrowingSignService")
public class AllBorrowingSignServiceImpl implements AllBorrowingSignService {

    @Resource
    SignMapper signMapper;
    @Resource
    InvestmentRecordMapper investmentRecordMapper;

    /**
     * 所有借款标页面
     * 查询满足条件的所有借款标
     *
     * @return
     */
    @Override
    public List findAll() {

        return signMapper.findAllForBorSign();
    }

    /**
     * 所有借款标
     * 分页查询
     *
     * @param loan
     * @return
     */
    @Override
    public List findAllPage(Loan loan) {
        loan.setLimit((loan.getLimit() - 1) * loan.getPage());
        return signMapper.findAllByPageBorSign(loan);
    }

    /**
     * 所有借款标
     * 统计总记录数
     *
     * @return 总记录数
     */
    @Override
    public Integer countPage(Loan loan) {
        return signMapper.countPageForAllBorSign(loan);
    }

    /**
     * 所有借款标页面
     * 投资记录操作
     * @param num
     * @return
     */
    @Override
    public List findByNum(String num) {
        return investmentRecordMapper.findByNum(num);
    }

    /**
     * 所有借款标
     * 导出表格
     *
     * @param workbook 传入容器
     *                 传入条件 ***暂未实现
     */
    @Override
    public void export(XSSFWorkbook workbook) {
        //1.查询出所有的部门数据
        List<Map<String, Object>> investmentList = findAll();
        //2.创建XSSFWorkbook
//        XSSFWorkbook workbook = new XSSFWorkbook();
        //3.创建sheet
        XSSFSheet sheet = workbook.createSheet();
        //4.创建表头
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("序号");
        row.createCell(1).setCellValue("借款ID");
        row.createCell(2).setCellValue("借款方");
        row.createCell(3).setCellValue("借款方手机");
        row.createCell(4).setCellValue("借款名称");
        row.createCell(5).setCellValue("借款金额");
        row.createCell(6).setCellValue("年化利率");
        row.createCell(7).setCellValue("还款方式");
        row.createCell(8).setCellValue("期限");
        row.createCell(9).setCellValue("借款管理费月率");
        row.createCell(10).setCellValue("借款管理费");
        row.createCell(11).setCellValue("上架时间");
        row.createCell(12).setCellValue("开售时间");
        row.createCell(13).setCellValue("状态");
        //5.遍历集合填充每一行的数据
        for (int i = 0; i < investmentList.size(); i++) {
            Row r = sheet.createRow(i + 1);
            Loan investmentRecord = new Loan();
            r.createCell(0).setCellValue(i + 1);
            r.createCell(1).setCellValue(investmentList.get(i).get("num").toString());
            r.createCell(2).setCellValue(investmentList.get(i).get("borrower").toString());
            r.createCell(3).setCellValue(investmentList.get(i).get("phone").toString());
            r.createCell(4).setCellValue(investmentList.get(i).get("entitle").toString());
            r.createCell(5).setCellValue(investmentList.get(i).get("money").toString());
            r.createCell(6).setCellValue(investmentList.get(i).get("annual").toString());
            r.createCell(7).setCellValue(investmentList.get(i).get("repayment").toString());
            r.createCell(8).setCellValue(investmentList.get(i).get("deadline").toString());
            r.createCell(9).setCellValue(investmentList.get(i).get("monthly").toString());
            r.createCell(10).setCellValue(investmentList.get(i).get("fee").toString());
            //日期处理
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (investmentList.get(i).get("putawaytime") != null ){
                String sDate = df.format(investmentList.get(i).get("putawaytime"));
                r.createCell(11).setCellValue(sDate);
            }

            if (investmentList.get(i).get("putawaytime") != null ){
                String sDate2 = df.format(investmentList.get(i).get("saletime"));
                r.createCell(12).setCellValue(sDate2);
            }
            r.createCell(13).setCellValue(investmentList.get(i).get("sname").toString());

        }
    }
}
